import React from 'react'
import ReactDOM from 'react-dom'
import cx from 'classnames'
import style from './style'
import MP from './ModalPortal'

const renderSubtreeIntoContainer = ReactDOM.unstable_renderSubtreeIntoContainer
const ModalPortal = React.createFactory(MP)

export default
class Modal extends React.Component {

  componentDidMount() {
    this.node = document.createElement('div')
    document.body.appendChild(this.node)
    this.renderPortal(this.props)
  }

  componentWillReceiveProps(props) {
    this.renderPortal(props)
  }

  componentWillUnmount() {
    ReactDOM.unmountComponentAtNode(this.node);
    document.body.removeChild(this.node);
  }

  renderPortal(props) {
    this.portal = renderSubtreeIntoContainer(this, ModalPortal(props), this.node);
  }

	render() {
		return React.DOM.noscript()
	}
}
